package de.backtomedieval.game;

import java.util.ArrayList;

import de.backtomedieval.buildings.Academy;
import de.backtomedieval.main.Game;
import de.backtomedieval.research.AbstractResearch;
import de.backtomedieval.research.Technology;

public class Player {
	
	private City currentCity = null;
	private ArrayList<City> citys = new ArrayList<City>();
	private KI ki = null;
	private Technology technology;


	public Player(String name) {
		this.technology = new Technology();
	}
	
	public City addCity(String name) {
		City city = new City(name,this.technology);
		citys.add(city);
		if (currentCity==null) currentCity = city;
		return city;
	}
	

	public ArrayList<City> getCitys() {
		return citys;
	}



	public KI getKi() {
		return ki;
	}

	public City getCurrentCity() {
		return currentCity;
	}

	public void setCurrentCity(City currentCity) {
		this.currentCity = currentCity;
	}
	
	public void nextRound() {
		if (currentCity == null) return;
		for (int i=0; i<citys.size();i++) {
			citys.get(i).round();
			researchProgress(citys.get(i).getAcademy());
		}
		
		
		if (ki!=null) {
			ki.executeKI();
		}
	}
	
	public void researchProgress(Academy academy) {
		if(academy.isActive()) {
			AbstractResearch r = academy.getToResearch();
			if (r != null) {
				int researchPoints = academy.getLevel() * 10;
				int researchAfter = r.getResearchPoints()-researchPoints;
				if (researchAfter>0) {
					r.setResearchPoints(researchAfter);
				} else {
					academy.setToResearch(null);
					if (r.getResearchPoints()!=0) {
						r.setResearchPoints(0);
						r.levelup(technology);
					}
					

				}
			}
		}
	}
	
	
	public void setKI(KI ki) {
		this.ki = ki;
	}


	public Technology getTechnology() {
		return technology;
	}
	

	
	
	
	
}
